package com.jash.alipaytest;

import android.os.SystemClock;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class OrderInfoBuilder {
    private static final String PID = "";
    private static final String APP_ID = "";
    private static final String API_NAME = "com.alipay.account.auth";
    private static final String APP_NAME = "mc";
    private static final String PRODUCT_ID = "APP_FAST_LOGIN";
    private static final String SCOPE = "kuaijie";
    private static final String TARGET_ID = "";
    private static final String AUTH_TYPE = "AUTHACCOUNT";
    private static final String PRIVATE_KEY= "";
    private boolean isRSA2;
    private String subject;
    private float totalAmount;
    private String body;
    private String timeout_express;

    public OrderInfoBuilder(boolean isRSA2, String subject, float totalAmount) {
        this.isRSA2 = isRSA2;
        this.subject = subject;
        this.totalAmount = totalAmount;
    }

    public OrderInfoBuilder setBody(String body) {
        this.body = body;
        return this;
    }

    public OrderInfoBuilder setTimeout_express(String timeout_express) {
        this.timeout_express = timeout_express;
        return this;
    }

    public String build() {
        StringBuilder builder = new StringBuilder();
        JSONObject object = new JSONObject();
        try {
            if (timeout_express != null) {
                object.put("timeout_express", timeout_express);
            }
            object.put("product_code", "QUICK_MSECURITY_PAY");
            object.put("total_amount", String.format(Locale.getDefault(), "%.2f", totalAmount));
            object.put("subject", subject);
            if (body != null) {
                object.put("body", body);
            }
            object.put("out_trade_no", getOutTradeNo());
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        app_id=2015052600090779&biz_content={"timeout_express":"30m","product_code":"QUICK_MSECURITY_PAY","total_amount":"0.01","subject":"1","body":"我是测试数据","out_trade_no":"IQJZSRC1YMQB5HU"}
// &charset=utf-8&method=alipay.trade.app.pay&notify_url=http://domain.merchant.com/payment_notify&sign_type=RSA2&timestamp=2016-08-25 20:26:31&version=1.0
        String src = builder.append("app_id=").append(APP_ID)
                .append("&biz_content=").append(object.toString())
                .append("&charset=utf-8")
                .append("&method=alipay.trade.app.pay")
                .append("&notify_url=").append("")
                .append("&sign_type=").append(isRSA2 ? "rsa2" : "rsa")
                .append("&timestamp=").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
                .append("&version=1.0").toString();
        builder.append("&sign=").append(SignUtils.sign(src, PRIVATE_KEY, isRSA2));
        return builder.toString();
    }
    private String getOutTradeNo() {
        long l = SystemClock.currentThreadTimeMillis();
        int r = (int) (Math.random() * 1000);
        return String.format(Locale.getDefault(), "%d%03d", l, r);
    }
}
